package com.siren.tank;

import com.siren.tank.cor.ColliderChain;
import com.siren.tank.group.AbstractFactory;
import com.siren.tank.group.BaseTank;

import java.awt.*;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author : wangjichao
 * @description:
 * @date : 2020/8/6 11:04
 */
public class GameModel {

    private static final GameModel INSTANCE = new GameModel();

    public static GameModel getInstance() {
        return INSTANCE;
    }

    static {
        INSTANCE.init();
    }

    private void init() {
        int initEnemiesCount = Integer.parseInt(PropertyMgrEnum.INSTANCE.get("initEnemiesCount"));
        // init enemies
        for (int i = 0; i < initEnemiesCount; i++) {
            AbstractFactory.INSTANCE.createBadTank(300 + i * 80, 300, Dir.DOWN);
        }

        AbstractFactory.INSTANCE.createWall(200, 200, 400, 250);
        AbstractFactory.INSTANCE.createWall(600, 600, 800, 650);
        AbstractFactory.INSTANCE.createWall(600, 200, 800, 250);

        myTank = AbstractFactory.INSTANCE.createGoodTank(50, 50,Dir.DOWN);
    }

    private BaseTank myTank;

    private ArrayList<GameObject> gameObjects = new ArrayList<>();

    ColliderChain colliderChain = new ColliderChain();

    private GameModel() {}

    public void paint(Graphics g) {
        Color color = g.getColor();
        g.setColor(Color.WHITE);
//        g.drawString("子弹的数量：" + bulletList.size(), 10, 60);
//        g.drawString("敌军坦克的数量：" + enemies.size(), 10, 80);
//        g.drawString("爆炸的数量：" + enemies.size(), 10, 100);
        g.setColor(color);

        myTank.paint(g);

        for (int i = 0; i < gameObjects.size(); i++) {
            gameObjects.get(i).paint(g);
        }

        // 互相碰撞
        for (int i = 0; i < gameObjects.size(); i++) {
            for (int j = i+1; j < gameObjects.size(); j++) {
                GameObject o1 = gameObjects.get(i);
                GameObject o2 = gameObjects.get(j);
                colliderChain.collide(o1, o2);
            }
        }
    }

    public void add(GameObject gameObject) {
        gameObjects.add(gameObject);
    }

    public void remove(GameObject gameObject) {
        gameObjects.remove(gameObject);
    }

    public void control(Map<Integer, Boolean> keyEvents) {
        myTank.control(keyEvents);
    }


}
